# -*- coding: utf-8 -*-
from datetime import timedelta
from jms.dwd.sqs.dwd_yl_cn_mysql_lmdm_sys_network_electronic_fence_base_dt import jms_dwd__dwd_yl_cn_mysql_lmdm_sys_network_electronic_fence_base_dt
from jms.dim.dim_lmdm_sys_network import jms_dim__dim_lmdm_sys_network
from utils.alerts.yl_threeSegCodeOnFailue import yl_threeSegCodeOnFailure
from utils.alerts.yl_threeSegCodeOnSuccess import yl_threeSegCodeOnSuccess
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator

def kwargs():
    kwargs = {
        "db": "ai_dwd",
        "table": "cn_mysql_lmdm_sys_network_electronic_fence_change",
        "desc": "网点围栏变更数据",
        "taskid": "10020",
        "ifprivacy": 0,
        "warnignore": 1,
    }
    return kwargs

jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change = SparkSqlOperator(
    task_id='jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change',
    master='yarn',
    name='jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change_{{ execution_date| date_add(1) | cst_ds }}',
    sql='jms/aigroup/ai_dwd/cn_mysql_lmdm_sys_network_electronic_fence_change/execute.hql',
    executor_cores=2,
    executor_memory='3G',
    email=['yushuo@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool_slots=4,
    num_executors=10,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled'                  : 'true',  # 动态资源开启
          'spark.shuffle.service.enabled'                    : 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors'             : 24,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '1G',  # 堆外内存
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions.pernode': 3,  # 每天生成 3 个分区
              'hive.exec.max.dynamic.partitions': 500
              },
    yarn_queue='pyspark',
    execution_timeout=timedelta(minutes=60),
    on_success_callback=yl_threeSegCodeOnSuccess(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_info"),
    on_failure_callback=yl_threeSegCodeOnFailure(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_alert"),

)

jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change << [jms_dim__dim_lmdm_sys_network,
                                                                       jms_dwd__dwd_yl_cn_mysql_lmdm_sys_network_electronic_fence_base_dt]
